/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import negocio.Tarifa;


/**
 *
 * @author JAVA
 */
public class PersistenciaTarifa {
    
    public static int insertarTarifa(Tarifa t){
        
        int i=0;
        Connection con=null;
        Statement st=null;
        try {           
            
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
            st=con.createStatement();
            i=st.executeUpdate("insert into tarifa (id,descripcion,precioAlquiler,precioVenta,diasAlquiler) values "
                    + "(0,'"+t.getDescripcion()+"',"+t.getPrecioAlquiler()+","+t.getPrecioVenta()+","+t.getDiasAlquiler()+")");
            st.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        }
        return i;
 
    }
    
    public static List<Tarifa> devolverTarifa(){
        
        List t=new ArrayList<Tarifa>();
        Connection con=null;
        Statement st=null;
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
                st=con.createStatement();
                ResultSet r=st.executeQuery("select id,descripcion,precioAlquiler,precioVenta,diasAlquiler from tarifa where tarifa.baja='0'");
                while(r.next()){
                    Tarifa ta=new Tarifa(r.getInt("id"),r.getString("descripcion"),r.getFloat("precioAlquiler"),r.getFloat("precioVenta"),r.getInt("diasAlquiler"));
                    t.add(ta);
                }
            } catch (SQLException ex) {
                Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally{
                st.close();
                con.close();
                
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return t;
        }       
    }
    
    
        public static int modificarTarifa(Tarifa tarifa){
            int j=0;
            Connection con=null;
            Statement st=null;      
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
                st=con.createStatement();
                j=st.executeUpdate("update tarifa set descripcion='"+tarifa.getDescripcion()+"',precioAlquiler="+tarifa.getPrecioAlquiler()+",precioVenta="+tarifa.getPrecioVenta()+",diasAlquiler="+tarifa.getDiasAlquiler()+" where tarifa.id="+tarifa.getId());
                st.close();
                con.close();
        } catch (InstantiationException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaTarifa.class.getName()).log(Level.SEVERE, null, ex);
        }     
          return j;
        }
        
        
        public static Tarifa obtenerTarifa(int codigo){
        Tarifa t=null;
        Connection con=null;
        Statement st=null;
        try {   
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection("jdbc:mysql://192.168.1.18/videoclub","cibernos","1234");
            st=con.createStatement();
            ResultSet r=st.executeQuery("select id,descripcion,precioAlquiler,precioVenta,diasAlquiler from tarifa where tarifa.baja=0 and tarifa.id="+codigo);
            while(r.next()){
                t=new Tarifa(r.getInt("id"),r.getString("descripcion"),r.getFloat("precioAlquiler"),r.getFloat("precioVenta"),r.getInt("diasAlquiler"));
            }
            st.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersistenciaProveedor.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return t;
        }
        
        
    }
        
        
}
